#pragma once
#include<iostream>
#include<vector>

namespace my_Priority_queue
{
	//仿函数实现判断小于
	template<class T>
	struct less
	{
		bool operator()(const T& left, const T& right)
		{
			return left < right;
		}
	};

	//仿函数实现判断大于
	template<class T>
	struct greater
	{
		bool operator()(const T& left, const T& right)
		{
			return left > right;
		}
	};


	//通过仿函数可以自定义比较方法！相当于qsort传指针来说，方便易懂


	template<class T,class Container = std::vector<T>,class Comapre = less<T>>
	class priority_queue
	{
		typedef priority_queue<T, Container, Comapre> _priority_queue; //priority_queue对象类型
	public:
		priority_queue()
			:_c(Container()) //初始化底层容器对象
			,_com(Comapre()) //初始化比较函数对象
		{}

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
			: _c(Container())
			, _com(Comapre())
		{
			while (first != last)
			{
				push(*first);
				++first;
			}
		}

		void push(const T& val)
		{
			_c.push_back(val); //插入数据
			adjust_up(_c.size() - 1); //向上调整
		}

		void pop()
		{
			std::swap(_c[0], _c[_c.size() - 1]); //堆顶数据与容器尾数据交换
			_c.pop_back(); //容器尾删淘汰堆顶数据
			adjust_down(0); //从堆顶开始向下调整
		}

		size_t size() const { return _c.size(); }

		const T& top() const { return _c.front(); }

		bool empty() const { return _c.empty(); }

		void swap(_priority_queue& _pq) { _c.swap(_pq._c); }

	private:
		Container _c;
		Comapre _com;

		void adjust_up(size_t child) //向上调整
		{
			size_t parent = (child - 1) / 2;
			while (child > 0)
			{
				if (_com(_c[parent], _c[child]))
				{
					std::swap(_c[parent], _c[child]);
					child = parent;
					parent = (child - 1) / 2;
				} else break;
			}
		}

		void adjust_down(size_t parent) //向下调整
		{
			size_t child = (parent * 2) + 1;
			while (child < _c.size())
			{
				if (child + 1 < _c.size() &&_com( _c[child], _c[child+1])) { ++child; }
				
				if (_com(_c[parent], _c[child]))
				{
					std::swap(_c[parent], _c[child]);
					parent = child;
					child = (parent * 2) + 1;
				} else break;
			}
		}

	};
}